﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour, ISceneController, IUserAction {
    GameModel model;

    void Awake() {
        SSDirector director = SSDirector.getInstance();
        director.currentSceneController = this;
        model = new GameModel();
        director.currentSceneController.LoadResources();
    }
    void Start() {
        model.Init();
    }
    void Update() {
        model.Update(Time.deltaTime);
    }
    void Instantiate(GameObject prefab, string name) {
        GameObject rt = Instantiate<GameObject>(prefab, Vector3.zero, Quaternion.identity);
        rt.name = name;
        model.AddObject(name, rt);
    }
    public void LoadResources() {
        GameObject prefab_bank = Resources.Load<GameObject>("Prefabs/Bank");
        Instantiate(prefab_bank, "BankLeft");
        Instantiate(prefab_bank, "BankRight");

        Instantiate(Resources.Load<GameObject>("Prefabs/Boat"), "Boat");

        GameObject prefab_priest = Resources.Load<GameObject>("Prefabs/Priest");
        GameObject prefab_devil = Resources.Load<GameObject>("Prefabs/Devil");
        for (int i = 0; i < 3; ++i) {
            Instantiate(prefab_priest, "Priest" + i);
            Instantiate(prefab_devil, "Devil" + i);
        }

        model.SetActionManager(GetComponent<SSActionManager>());
    }

    public int CheckGameState() {
        return model.GetGameState();
    }
    public void Init() {
        model.Init();
    }
    public void Switch(GameObject obj) {
        if (model.GetGameState() != 0 || model.IsAnimating()) {
            return;
        }
        model.Switch(obj);
    }
    public void SetOff() {
        if (model.GetGameState() != 0 || model.IsAnimating()) {
            return;
        }
        model.SetOff();
    }
}
